import Common from '../common'
var Js = Common('coupon')
export default {
  name: 'coupon',
  mixins: [Js],
  components: {},
  data() {
    return {
      fields: [{
          key: 'id',
          label: 'ID',
          application: [],
          componentName: 'sls-input',
          listType: '',
          width: 50
        },
        {
          key: 'coupon_no',
          label: '优惠券NO',
          application: [],
          componentName: 'sls-input',
          listType: '',
          width: 50
        },
        {
          key: 'title',
          label: '名称',
          application: ['编辑', '添加'],
          componentName: 'sls-input',
          listType: 'normal',
          header_search: {
            componentName: 'sls-input',
            style: 'width:160px;margin-right:2px;',
            placeholder: '请输入名称',
            clearable: true,
            defaultValue: '',
            optionsName: '',
            changeFunc: function(val, self) {
              if (val) {
                self.searchItem.title = ['LIKE', ['%' + val + '%']];
              } else {
                delete self.searchItem.title;
              };
            },
          },
        },
        {
          key: 'type',
          label: '类型',
          application: ['编辑', '添加'],
          formatter: function(val, tests) {
            var testArray = ['未完', '完整']
            return testArray[val.type];
          },
          componentName: 'sls-select',
          optionsName: 'typeOptions',
          listType: '',
          limit: 10,
          defaultProps: {
            label: 'text',
            value: 'value',
          },
          placeholder: '请选择类型',
          width: 50,
          header_search: {
            componentName: 'sls-select',
            optionsName: 'typeOptions',
            style: 'width:160px;margin-right:2px;',
            placeholder: '请选择类型',
            changeFunc: function(val, self) {
              if (val) {
                self.searchItem.type = val;
              } else {
                delete self.searchItem.type;
              };

            },
          }
        },
        {
          key: 'coupon_type',
          label: '领取场景',
          application: ['编辑', '添加'],
          formatter: function(val, tests) {
            var testArray = ['未完', '完整']
            return testArray[val.coupon_type];
          },
          componentName: 'sls-select',
          optionsName: 'coupon_typeOptions',
          listType: '',
          limit: 10,
          defaultProps: {
            label: 'text',
            value: 'value',
          },
          placeholder: '请选择领取场景',
          width: 50,
          header_search: {
            componentName: 'sls-select',
            optionsName: 'coupon_typeOptions',
            style: 'width:160px;margin-right:2px;',
            placeholder: '请选择领取场景',
            changeFunc: function(val, self) {
              if (val) {
                self.searchItem.coupon_type = val;
              } else {
                delete self.searchItem.coupon_type;
              };

            },
          }
        },
        {
          key: 'limit_price',
          label: '领取满足金额',
          application: ['编辑', '添加'],
          componentName: 'sls-input',
          listType: 'normal',
        },
        {
          key: 'condition',
          label: '限额（满减）',
          application: ['编辑', '添加'],
          componentName: 'sls-input',
          listType: 'normal',
        },
        {
          key: 'value',
          label: '抵扣额',
          application: ['编辑', '添加'],
          componentName: 'sls-input',
          listType: 'normal',
        },
        {
          key: 'score',
          label: '积分兑换',
          application: ['编辑', '添加'],
          componentName: 'sls-input',
          listType: 'normal',
        },
        {
          key: 'shop_no',
          label: '商户名称',
          application: ['添加', '编辑'],
          componentName: 'sls-input',
          formatter: function(val, tests) {

            return val.shop.shop_name;
          },
          listType: 'normal',
          dialog: {
            suggest: true,
            querySearch: function(val, self) {
              if (val) {
                var postData = {
                  searchItem: {
                    shop_name: ['like', '%' + val + '%'],
                    status: 1
                  }
                }
              };
              var res = self.initShopData(postData);
              return res;
            },
          },
          header_search: {
            componentName: 'sls-input',
            placeholder: '请输入商户名称',
            suggest: true,
            querySearch: function(val, self) {
              console.log('querySearch')
              if (val) {
                var postData = {
                  searchItem: {
                    shop_name: ['like', '%' + val + '%'],
                    status: 1
                  }
                };
                var res = self.initShopData(postData);
                return res;
              } else {
                delete self.searchItem.shop_no;
              }
            },
            changeFunc: function(val, self) {
              if (val) {
                self.searchItem.shop_no = val;
              } else {
                delete self.searchItem.shop_no;
              };

            },
          }
        },
        {
          key: 'stock',
          label: '库存',
          application: ['编辑', '添加'],
          componentName: 'sls-input',
          listType: 'normal',
        },
        {
          key: 'limit',
          label: '领取数量限制',
          application: ['编辑', '添加'],
          componentName: 'sls-input',
          listType: 'normal',
        },
        {
          key: 'use_limit',
          label: '使用数量限制',
          application: [],
          componentName: 'sls-input',
          listType: '',
        },
        {
          key: 'time',
          label: '有效期(天)',
          application: ['编辑', '添加'],
          componentName: 'sls-input',
          listType: 'normal',
          formatter: function(val) {
            return val.time + '天'
          }
        },
        {
          key: 'create_time',
          label: '创建时间',
          listType: 'normal',
          placeholder: '请选择创建时间',
          header_search: {
            componentName: 'sls-date-time-range',
            style: 'width:160px;margin-right:2px;',
            start_placeholder: '创建开始时间',
            end_placeholder: '创建结束时间',
            changeFunc: function(value, self) {
              if (!value) {
                delete self.searchItem.create_time;
              } else {
                self.searchItem.create_time = ['between', value = value.map(function(e) {
                  return e / 1000;
                })]
              };
            },
          },
          width: 200,
        },
        {
          label: '操作',
          listType: 'deal',
          width: 80
        },
      ],
      // 按钮配置
      btn_info: [


        {
          type: 'info',
          icon: 'edit',
          size: 'mini',
          position: 'list',
          text: function(data) {
            return '编辑'
          },
          func: {
            apiName: function(self) {
              return "api_couponUpdate"
            },
            formData: function(self) {
              return self.formData;
            },
            postData: function(self) {
              if (self.submitData.category_id == 0) {
                return false;
              };
              // self.submitData.valid_time = self.submitData.time ? self.submitData.time * 86400 * 1000 : self
              //   .formData.valid_time;
              var postData = {
                searchItem: {
                  id: self.formData.id,
                },
                data: self.submitData
              };
              return postData;
            }
          },
        },
        {
          type: 'danger',
          icon: 'delete',
          size: 'mini',
          funcType: 'submit',
          position: 'header',
          text: function(data) {
            return '删除选中'
          },
          func: {
            apiName: function(self) {
              return "api_couponUpdate"
            },
            postData: function(self) {
              var deleteArray = [];
              for (var i = 0; i < self.selectionArray.length; i++) {
                deleteArray.push(self.selectionArray[i].id);
              };
              var postData = {
                searchItem: {
                  id: ['in', deleteArray],
                },
                data: {
                  status: -1
                }
              };
              return postData;
            }
          },
        },
        {
          type: 'info',
          icon: 'edit',
          size: 'mini',
          position: 'header',
          text: function(data) {
            return '添加'
          },
          func: {
            apiName: function(self) {
              return "api_couponAdd"
            },
            formData: function(self) {
              var data = {
                title: '',
                description: '',
                price: '',
                mainImg: [],
                bannerImg: [],
              };
              return data
            },
            postData: function(self) {
              // self.submitData.valid_time = self.submitData.time ? self.submitData.time * 86400 * 1000 : '';

              if(!self.submitData.type){
                self.submitData.type = 1;
              };
              
              if(!self.submitData.coupon_type){
                self.submitData.coupon_type = 1;
              };

              self.submitData.limit = 0;
              self.submitData.behavior = 0;
              self.submitData.stock = 999999;
              var postData = {
                data: self.submitData
              };
              return postData;
            }
          },
        },
      ],


      searchItem: {

      },
      startSearchItem: {

      },
      optionData: {
        shopOptions: [],
        behaviorOptions: [{
          text: '通用券',
          value: 1
        }, {
          text: '指定券',
          value: 2
        }],
        typeOptions: [{
          text: '抵扣券',
          value: 1
        }, {
          text: '折扣券',
          value: 2
        }],
        coupon_typeOptions: [{
          text: '新人获得',
          value: 1
        }, {
          text: '消费获得',
          value: 2
        }, {
          text: '赠送获得',
          value: 3
        }, {
          text: '直接领取',
          value: 4
        }]
      },
      otherData: {},
      UserInfo: {
        tableName: 'UserInfo',
        searchItem: {},
        key: 'user_no',
        middleKey: 'user_no',
        condition: 'in',
      },

    }

  },

  mounted() {
    const self = this;
    self.table_arguments.tree_props = {
      children: 'child',
      child: 'child',
      hasChildren: 'child'
    }
    self.searchData = self.$$cloneForm(self.searchItem);
    this.init()
  },
  computed: {
    token: function() {
      return this.$store.getters.getToken
    }
  },
  methods: {

    /**
     * 初始化
     */
    init() {
      this.initMainData();
    },

    async initShopData(postData) {

      const self = this;
      var res = await self.$$api_shopGet({
        data: postData
      });
      var data = [];
      if (res.info.data.length > 0) {
        for (var i = 0; i < res.info.data.length; i++) {
          data.push({
            value: res.info.data[i]['shop_name'],
            shop_no: res.info.data[i]['shop_no'],
          })
        };
        console.log('shop', data)
        self.optionData.shopOptions = data;
        return data;
      } else {
        return [{
          "value": "无",
          "shop_no": ""
        }];
      };


    },


    /**
     * 列表主函数
     */
    async initMainData(isNew) {

      const self = this;
      self.table_arguments.loading = true;
      const postData = {};
      if (isNew) {
        self.paginate.currentPage = 1;
      };
      postData.paginate = self.$$cloneForm(self.paginate);
      postData.token = self.$store.getters.getToken;
      if (self.searchItem) {
        postData.searchItem = self.$$cloneForm(self.searchItem)
      };
      if (JSON.stringify(self.getBefore) != "{}") {
        postData.getBefore = self.$$cloneForm(self.getBefore);
      };
      postData.getAfter = {
        shop: {
          tableName: 'Shop',
          middleKey: 'shop_no',
          key: 'shop_no',
          condition: '=',
          info: ['shop_name', 'id'],
        }
      }
      var res = await self.$$api_couponGet({
        data: postData
      });
      self.mainData = res.info.data;
      self.paginate.count = res.info.total;
      self.table_arguments.loading = false;

    },

  },


}
